package com.slh.touchandroid.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.slh.touchandroid.R;
import com.slh.touchandroid.activity.TouchActivity;
import com.slh.touchandroid.adapter.FunctionApdater;
import com.slh.touchandroid.db.DBHelper;
import com.slh.touchandroid.model.AppInfo;
import com.slh.touchandroid.model.FloatInfo;
import com.slh.touchandroid.receiver.Broadcast;
import com.slh.touchandroid.utils.AppUtils;
import com.slh.touchandroid.utils.CONFIG;

public class PanelFrament extends BaseFragment {
	public static final String MAIN_PANEL = "com.slh.touchandroid.fragment.PanelFrament.MAIN_PANEL";
	public static final String SUB_PANEL = "com.slh.touchandroid.fragment.PanelFrament.SUB_PANEL";
	public static final String FAV_PANEL = "com.slh.touchandroid.fragment.PanelFrament.FAV_PANEL";
	public static final String KEY_TYPE = "com.slh.touchandroid.fragment.PanelFrament.KEY_TYPE";
	public static final String KEY_TITLE = "com.slh.touchandroid.fragment.PanelFrament.KEY_TITLE";
	private String type;

	public static PanelFrament newInstance(String type, String title) {
		PanelFrament frament = new PanelFrament();
		Bundle bundle = new Bundle();
		bundle.putString(KEY_TYPE, type);
		bundle.putString(KEY_TITLE, title);
		frament.setArguments(bundle);
		return frament;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		type = getArguments().getString(KEY_TYPE);
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.view_touch_main, container,
				false);

		if (SUB_PANEL.equals(type)) {
			rootView = inflater.inflate(R.layout.view_touch_sub, container,
					false);
			loadSub(rootView);
		} else if (FAV_PANEL.equals(type)) {
			rootView = inflater.inflate(R.layout.view_touch_favorite,
					container, false);
			loadFav(rootView);
		} else {
			loadMain(rootView);
		}
		return rootView;
	}

	private void loadFav(View rootView) {
		for (int i = 0; i < 9; i++) {
			AppInfo info = DBHelper.getInstance(getActivity()).findFavorite(
					getActivity(), i);
			ImageButton fav = (ImageButton) rootView
					.findViewWithTag(getString(R.string.tag_favorite) + i);
			if (null == info) {
				fav.setImageResource(R.drawable.btn_add);
			} else {
				fav.setImageDrawable(AppUtils.getApplicationIcon(getActivity(),
						info.pgk));
			}
			fav.setOnClickListener(mFavClickListener);
		}

	}

	private void loadMain(View rootView) {
		DBHelper dbHelper = DBHelper.getInstance(getActivity());
		for (int i = 0; i < 9; i++) {
			int id = getResources().getIdentifier("img" + i, "id",
					getActivity().getPackageName());
			ImageButton view = (ImageButton) rootView.findViewById(id);
			FloatInfo info = dbHelper.findMain(getActivity(), i);
			if (info != null) {

				view.setTag(info.tag);
				view.setImageResource(info.resource);
			} else {
				view.setImageResource(R.drawable.btn_add);
			}
		}

	}

	private void loadSub(View rootView) {
		DBHelper dbHelper = DBHelper.getInstance(getActivity());
		for (int i = 0; i < 12; i++) {
			int id = getResources().getIdentifier("imgsub" + i, "id",
					getActivity().getPackageName());
			ImageButton view = (ImageButton) rootView.findViewById(id);
			FloatInfo info = dbHelper.findSettings(getActivity(), i);
			if (info != null) {

				view.setTag(info.tag);
				view.setImageResource(info.resource);
			} else {
				view.setImageResource(R.drawable.btn_add);
			}
		}

	}

	private OnClickListener mFavClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String tag = (String) v.getTag();
			int position = Integer.valueOf(tag.substring("tag_favorite"
					.length()));
			AppInfo info = DBHelper.getInstance(getActivity()).findFavorite(
					getActivity(), position);
			if (null == info) {
				Bundle bundle = new Bundle();
				bundle.putInt(TouchActivity.EXTRA_PICK_APP_POSITION, position);
				TouchActivity.start(getActivity(),
						TouchActivity.ACTION_PICK_APP, bundle);
			}

		}
	};

	public void onClick(final View view) {
		final DialogFragment dialogFragment = new DialogFragment() {
			@Override
			public View onCreateView(LayoutInflater inflater,
					ViewGroup container, Bundle savedInstanceState) {
				View contentView = inflater.inflate(R.layout.view_add_list,
						container, false);
				final ArrayList<FloatInfo> floatInfos = getAllFunctions();
				ListView lv = (ListView) contentView
						.findViewWithTag(getString(R.string.tag_list));
				FunctionApdater apdater = new FunctionApdater(getActivity(),
						R.layout.row_common_vertical, floatInfos);
				lv.setAdapter(apdater);
				lv.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View v,
							int position, long id) {
						String action = null;
						FloatInfo info = floatInfos.get(position);
						((ImageButton) view).setImageResource(info.resource);
						if (info.tag.equals(getString(R.string.tag_add))) {
							if (MAIN_PANEL.equals(type)) {
								DBHelper.getInstance(getActivity())
										.deleteMainWithPosition(getActivity(),
												position);
								action = CONFIG.BROADCAST.ACTION_MAIN_PANEL_CHANGED;

							} else if (SUB_PANEL.equals(type)) {
								DBHelper.getInstance(getActivity())
										.deleteSettingsWithPosition(
												getActivity(), position);
								action = CONFIG.BROADCAST.ACTION_SUB_PANEL_CHANGED;
							}
						} else {
							if (MAIN_PANEL.equals(type)) {
								info.position = Integer.valueOf(view
										.getContentDescription().toString());
								DBHelper.getInstance(getActivity()).insertMain(
										getActivity(), info);
								view.setTag(info.tag);
								action = CONFIG.BROADCAST.ACTION_MAIN_PANEL_CHANGED;
							} else if (SUB_PANEL.equals(type)) {
								info.position = Integer.valueOf(view
										.getContentDescription().toString());
								DBHelper.getInstance(getActivity())
										.insertSettings(getActivity(), info);
								view.setTag(info.tag);
								action = CONFIG.BROADCAST.ACTION_SUB_PANEL_CHANGED;
							}
						}
						Broadcast.sendBroadcast(getActivity(), action);
						dismiss();

					}
				});
				return contentView;
			}

			@Override
			public void onCreate(Bundle savedInstanceState) {
				setStyle(STYLE_NO_TITLE, 0);
				super.onCreate(savedInstanceState);
			}

		};

		dialogFragment.show(getFragmentManager(), "");

	}

	private ArrayList<FloatInfo> getAllFunctions() {
		ArrayList<FloatInfo> floatInfos = new ArrayList<FloatInfo>();
		floatInfos.add(new FloatInfo(getString(R.string.tag_add),
				R.drawable.btn_add, "None"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_airplane),
				R.drawable.btn_airplane, "Airplane Mode"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_app),
				R.drawable.btn_app, "Apps"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_bluetooth),
				R.drawable.btn_bluetooth, "Bluetooth"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_camera),
				R.drawable.btn_camera, "Camera"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_data),
				R.drawable.btn_data, "Mobile Data"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_folder),
				R.drawable.btn_folder, "File Manager"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_gps),
				R.drawable.btn_gps, "GPS"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_home),
				R.drawable.btn_home, "Home"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_light),
				R.drawable.btn_light, "Torch"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_lock),
				R.drawable.btn_lock, "Lock"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_mirror),
				R.drawable.btn_mirror, "Mirror"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_recent),
				R.drawable.btn_recent, "Recent Apps"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_rotate),
				R.drawable.btn_rotate, "Auto Rotation"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_wifi),
				R.drawable.btn_wifi, "WIFI"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_sound),
				R.drawable.btn_volume_on, "Sound"));
		floatInfos.add(new FloatInfo(getString(R.string.tag_settings),
				R.drawable.btn_settings, "Settings App"));

		return floatInfos;
	}

}
